package cn._2dland.leech;

import main.TaskReader;
import cn._2dland.util.Logger;

public class Worker {

	private String savePath;

	public Worker(String savePath) {
		this.savePath = savePath;
	}

	public void exec(String taskFile) {
		TaskReader tr = new TaskReader();
		tr.open(taskFile);

		Logger.info("开始执行任务文件: " + taskFile);
		Logger.info("下载的文件将保存在: " + savePath);

		for(String line; (line=tr.readTask()) != null; ) {
			Logger.debug("Task: " + line);

			LeechClient client = LeechFactory.getLeech(line);
			if(client == null) {
				Logger.error("No leech support this url...");
				continue;
			}

			Logger.debug(client.getClass().getName());

			if(client.download(line, savePath)) {
				Logger.info("successed: " + line);
			} else {
				Logger.error("failed: " + line);
			}
		}

		tr.close();
	}
}
